package edu.szu.huhanpeng.doodlingFight.controller.dataController.exchange
{
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.model.ChessBoardProxy;
	import edu.szu.huhanpeng.doodlingFight.model.ChessGroupProxy;
	import edu.szu.huhanpeng.doodlingFight.model.data.ChessData;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * 刷全盘寻找已经组成星的棋子，推入groupProxy的done.
	 * @author Huhanpeng
	 */
	public class FindDoneDc extends SimpleCommand implements ICommand, INotifier
	{
		
		override public function execute(note:INotification):void
		{
			var chessBoard:ChessBoardProxy = facade.retrieveProxy(ChessBoardProxy.NAME) as ChessBoardProxy;
			var groupProxy:ChessGroupProxy = facade.retrieveProxy( ChessGroupProxy.NAME ) as ChessGroupProxy;
			var index:int = 0;
			var checkChess:IChessData;
			var tChess:IChessData;
			var count:int;
			var doneVec:Vector.<IChessData>;
			
			//寻找水平可消
			while ( index < chessBoard.size * chessBoard.size - 2 )
			{
				checkChess = chessBoard.getChessByIndex( index );
				count = 1;
				
				tChess = checkChess.friend[ChessData.SIX];
				index++;
				while ( tChess != null )
				{
					count++;
					index++;
					tChess = tChess.friend[ChessData.SIX];
				}
				
				if ( count > 2 ) 
				{
					doneVec = new Vector.<IChessData>();
					var tracer:Array = new Array();
					while ( count > 0)
					{
						doneVec.push( checkChess );
						tracer.push(checkChess.square.index);
						checkChess = checkChess.friend[ChessData.SIX];
						count--;
					}
					trace("shuiping" + tracer);
					groupProxy.addDone( doneVec );
				}
			}

			//寻找垂直可消
			index = 0;
			var column:int;
			while ( true ) 
			{
				checkChess = chessBoard.getChessByIndex( index );
				count = 1;
				
				tChess = checkChess.friend[ChessData.EIGHT];
				index+=chessBoard.size;
				while (tChess != null) 
				{
					count++;
					index+=chessBoard.size;
					tChess = tChess.friend[ChessData.EIGHT];
				}
				
				if ( count > 2 ) 
				{
					doneVec = new Vector.<IChessData>();
					var bmm:Array = new Array();
					while ( count > 0 ) 
					{
						doneVec.push( checkChess );
						bmm.push(checkChess.square.index);
						checkChess = checkChess.friend[ChessData.EIGHT];
						count--;
					}
					trace("垂直" + bmm);
					groupProxy.addDone( doneVec );
				}
				
				
				if ( Math.floor(index / chessBoard.size) > chessBoard.size - 3 ) 
				{
					column = index % chessBoard.size;
					if ( column == chessBoard.size - 1 ) 
					{
						//最后一列跳出
						break;
					}
					else
					{
						//转下一列的列头
						index = column + 1;
					}
				}
			}
		}
	}
}